package org.september.pisces.region.service;

import java.util.List;

import org.september.pisces.cache.aop.SimpleCache;
import org.september.pisces.region.entity.Region;
import org.september.smartdao.CommonDao;
import org.september.smartdao.model.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RegionService {

	public static Region activeRegion;
	
	@Autowired
	private CommonDao dao;
	
    public String getFullRegionId(Long regionId) {
        if (regionId == null) {
            return null;
        }
        String result = regionId.toString();
        Long parentId = null;
        do {
            parentId = getParentId(regionId);
            if (parentId != null) {
                result = parentId + "-" + result;
            }
            regionId = parentId;
        } while (parentId != null);
        return result;
    }

    private Long getParentId(Long regionId) {
        if (regionId == -1) {
            return null;
        }
        Region po = dao.get(Region.class, regionId);
        if (po == null) {
            return null;
        }
        if (po.getParentId() == -1) {
            return null;
        }
        return po.getParentId();
    }

    
    public Region getParent(Long regionId) {
        if (regionId == -1) {
            return null;
        }
        Region po = dao.get(Region.class, regionId);
        if (po == null) {
            return null;
        }
        if (po.getParentId() == -1) {
            return null;
        }
        Region parent = dao.get(Region.class, po.getParentId());
        return parent;
    }
    /**
     * 修改区域启动设置
     * @param region
     * @param enableFlag
     */
    @Transactional
    public void setRegionEnable(Region region, Integer enableFlag) {
        // 下级启用，自动启用上级
        if (enableFlag==1){
            updateParent(region);
        }
        updateChild(region, enableFlag);

    }
    public void updateChild(Region region, Integer enableFlag){
        Region example = new Region();
        example.setParentId(region.getId());
        List<Region> regions = dao.listByExample(example);
        // 下面的区域与上级一致
        if (!regions.isEmpty()) {
            regions.parallelStream().forEach(item -> updateChild(item, enableFlag));
        }
        region.setEnableFlag(enableFlag);
        dao.update(region);

    }
    public void updateParent(Region region){
        if (region.getParentId()!=-1){
            Region parent = dao.get(Region.class, region.getParentId());
            updateParent(parent);
        }
        region.setEnableFlag(1);
        dao.update(region);

    }

    public List<Region> listByParams(ParamMap paramMap) {
    	return dao.listByParams(Region.class, "Region.list", paramMap);
    }
    
    public String getEnableRegionIds() {
    	Region vo = new Region();
    	vo.setDeleteFlag(0);
    	vo.setEnableFlag(1);
    	List<Region> list = dao.listByExample(vo);
    	StringBuilder sb = new StringBuilder();
    	for(Region r : list) {
    		sb.append(r.getId()).append(",");
    	}
    	return sb.substring(0, sb.length()-1);
    }
    
    //目前返回区县一级的id
    public String getRegionChildrenIds(Long parentId) {
    	Region vo = new Region();
    	vo.setDeleteFlag(0);
    	vo.setEnableFlag(1);
    	vo.setParentId(parentId);
    	ParamMap pm = new ParamMap();
    	pm.put("parentId", parentId);
    	List<Region> list = dao.listByParams(Region.class, "Region.selectTownsOfParent", pm);
    	if(list.isEmpty()) {
    		return String.valueOf(parentId);
    	}
    	StringBuilder sb = new StringBuilder();
    	for(Region r : list) {
    		sb.append(r.getId()).append(",");
    	}
    	sb.append(parentId);
    	return sb.toString();
    }
    
    @SimpleCache(timeoutSec = 3600)
    public String getRegionName(Long id) {
    	Region po = dao.get(Region.class, id);
    	if(po!=null) {
    		return po.getName();
    	}
    	return "";
    }
}
